
* Admin data

capture program drop balance_fig1
program define balance_fig1, rclass
qui {
  syntax varlist [, cluster(string) data(string)]
  if "`cluster'"=="" local se = ""
  if "`cluster'"!="" local se = "cluster(`cluster')"
  tempname memhold
  postfile `memhold' str50(var) b_T0 b_T1 pvalue1 pvalue2 pvalue3 pvalue4 pvalue5 N1 N2 N3 using "$path/Auxiliary/results/balance_`data'.dta", replace
  foreach var in `varlist'{	
	qui sum `var' if T==0    //Control mean
    local b_T0 = r(mean)
	qui sum `var' if T==1    //Treatment mean
    local b_T1 = r(mean)

	qui areg `var' T, a(strata) `se' 
		local N1 = e(N)
		local p1 = r(table)[4,1]

	qui areg `var' T if guardian_completed==1 & constant==1, a(strata) `se' 
		local N2 = e(N)
		local p2 = r(table)[4,1]

	qui areg `var' T if student_completed==1 & constant==1, a(strata) `se' 
		local N3 = e(N)
		local p3 = r(table)[4,1]		

	qui areg `var' T TR if constant==1, a(strata) `se' 
		local p4 = r(table)[4,1]		
		local p5 = r(table)[4,2]		
		
	post `memhold' ("`var'") (`b_T0') (`b_T1') (`p1') (`p2') (`p3') (`p4') (`p5') (`N1') (`N2') (`N3')
  }
   postclose `memhold'
	 
	 
  preserve
  use  "$path/Auxiliary/results/balance_`data'.dta", clear   
	 
   // Label to be able to export later
   label var var "Variable"
   label var b_T0 "Mean Control"
   label var b_T1 "Mean Treatment"
   label var N1 "N"

   sleep 10000
   save "$path/Auxiliary/results/balance_`data'.dta", replace
  restore
}
end


  
/*





********************************************************************************
********************************************************************************

* Balance Figure

capture program drop balance_figure
program define balance_figure
qui {

  use "$output/1.Tables/balance_fig.dta", clear
  append using "$output/1.Tables/balance_fig2.dta"
  replace var="Years of experience" if regex(var,"P7. Years of experience")==1

  ren b_T0 T02
  ren b_T1 T03
  ren b_T2 T04
  ren N T01
  ren pvalue1 T07
  ren pvalue2 T08
  forvalues j=9/12 {
    gen T`j'=.
  }
  insobs 2, before(1)
  replace var="blank" in 1
  replace var="Panel A. Student characteristics" in 2
  insobs 2, before(13)
  replace var="blank" in 13
  replace var="Panel B. School characteristics" in 14
  insobs 2, before(20)
  replace var="blank" in 20
  replace var="Panel C. Teacher characteristics" in 21
  drop sample
  gen aux=_n
  replace T9=T07 if aux>=15 & aux<=19
  replace T07=. if T9!=.
  replace T10=T07 if aux>=22 & aux<=25
  replace T07=. if T10!=.
  replace T11=T08 if aux>=15 & aux<=19
  replace T08=. if T11!=.
  replace T12=T08 if aux>=22 & aux<=25
  replace T08=. if T12!=.
  
  drop aux
  order var T02 T03 T04 T07 T08 T9 T10 T11 T12 T01
  format T02 T03 T04 %9.3fc
   
  tempvar _aux5  
  qui gen     `_aux5'=var+"       "+string(T03, "%9.3f")  if  T03<0
  qui replace `_aux5'=var+"        "+string(T03, "%9.3f")   if T03<1	  & T03>=0
  qui replace `_aux5'=var+"        "+string(T03, "%9.3f")   if T03<10   & T03>=1
  qui replace `_aux5'=var+"      "+string(T03, "%9.3f")     if T03<100  & T03>=10
  qui replace `_aux5'=var+"    "+string(T03, "%9.3f")      if T03<1000 & T03>=100
  qui replace `_aux5'=var+"    "+string(T03, "%9.3f")      if T03>=1000
  
  qui replace `_aux5'=`_aux5'+"       "+string(T04, "%9.3f")   if T04<0  
  qui replace `_aux5'=`_aux5'+"        "+string(T04, "%9.3f")  if  T04<1    &  T04>=0
  qui replace `_aux5'=`_aux5'+"        "+string(T04, "%9.3f")  if  T04<10   &  T04>=1
  qui replace `_aux5'=`_aux5'+"      "+string(T04, "%9.3f")    if  T04<100  &  T04>=10
  qui replace `_aux5'=`_aux5'+"    "+string(T04, "%9.3f")     if  T04<1000 &  T04>=100
  qui replace `_aux5'=`_aux5'+"    "+string(T04, "%9.3f")     if  T04>=1000
  
  
  qui replace `_aux5'=`_aux5'+"       "+string(T02, "%9.3f")   if T02<0  
  qui replace `_aux5'=`_aux5'+"        "+string(T02, "%9.3f")  if  T02<1    &  T02>=0
  qui replace `_aux5'=`_aux5'+"        "+string(T02, "%9.3f")  if  T02<10   &  T02>=1
  qui replace `_aux5'=`_aux5'+"      "+string(T02, "%9.3f")    if  T02<100  &  T02>=10
  qui replace `_aux5'=`_aux5'+"    "+string(T02, "%9.3f")     if  T02<1000 &  T02>=100
  qui replace `_aux5'=`_aux5'+"    "+string(T02, "%9.3f")     if  T02>=1000
			
  qui replace `_aux5'=`_aux5'+"           "+string(T01, "%9.0f")    if T01<10   & T01>=1
  qui replace `_aux5'=`_aux5'+"          "+string(T01, "%9.0f")     if T01<100  & T01>=10
  qui replace `_aux5'=`_aux5'+"        "+string(T01, "%9.0f")      if T01<1000 & T01>=100
  qui replace `_aux5'=`_aux5'+"      "+string(T01, "%9.0f")       if T01>=1000
  
  qui replace `_aux5'=var+"       "+"      "+"      " if var=="Panel A. Student characteristics"
  qui replace `_aux5'=var+"       "+"    "+"     " if var=="" | var=="Panel B. School characteristics"
  qui replace `_aux5'=var+"       "+"    "+"     " if var=="" | var=="Panel C. Teacher characteristics"
  qui replace `_aux5'="       "+"       "+"      "+"." if var=="" | var=="blank"
	
  tempvar order
  gen `order'=_n
  labmask `order', values(`_aux5')
  
  local control "Control"
  local T1 "Tracking"
  local T2 "Bimodal"
  local obs "Obs."
  count
  local n_var=r(N)
  cd "$output/2.Figures/"
  # delimit ;		
    twoway (scatter `order' T07, mcolor(teal) msize(small) msymbol(smcircle))
           (scatter `order' T9, mcolor(dkorange) msize(small) msymbol(smdiamond))
		   (scatter `order' T10, mcolor(cranberry) msize(small) msymbol(triangle))
		   (scatter `order' T08, mcolor(ebblue) msize(small) msymbol(circle_hollow))
           (scatter `order' T11, mcolor(navy) msize(small) msymbol(smdiamond_hollow))
		   (scatter `order' T12, mcolor(gray) msize(small) msymbol(triangle_hollow)),		     
		   xlabel(0 0.05 0.1 1, notick labs(vsmall) ) 
		   xtitle("p-value", size(small)) 
		   ylabel(1(1)`n_var', val notick labs(vsmall) format(%9.2f) 
		   labgap(*3) angle(horizontal)) yscale(rev r(1(1)`n_var')) 
		   ytitle("")
           xline(0.05, lp(dot) lc(black) lw(medthin))
		   xline(0.1, lp(dot) lc(black) lw(medthin))
		   graphr(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white))  
		   legend(off)	   
		   text(0.67 -0.84 "`T1'" , si(vsmall)) 
		   text(0.67 -0.60 "`T2'" , si(vsmall))
		   text(0.67 -0.37 "`control'", si(vsmall))
		   text(0.67 -0.16 "`obs'", si(vsmall))
		   saving("$output/2.Figures/balance_fig.gph", replace);
		   graph2tex, epsfile(balance_fig);
  # delimit cr
  
 * erase "$output/1.Tables/balance_fig.dta"
 * erase "$output/1.Tables/balance_fig2.dta"

}
end
